package cn.col.gszf.service.impl;

import cn.col.gszf.mapper.UserMapper;
import cn.col.gszf.model.UserDetail;
import cn.col.gszf.pojo.User;
import cn.col.gszf.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description
 * @Author zhaoning
 * @Date 2024/6/25
 */

@Service
@Slf4j
public class UserServiceImpl implements UserService, UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        log.error("userName = " + userName);
        QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("user_name", userName);

        User user = userMapper.selectOne(wrapper);

        if (user == null) {
            throw new BadCredentialsException("用户名不存在或密码错误！");
        }
        return new UserDetail(user);
    }

    @Override
    public boolean vote() {
        return true;
    }
}
